package at.bubblebird.todoapp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.*;

import android.os.AsyncTask;

public class LoginAsyncTask extends AsyncTask<URL, String, String> {

	@Override
	protected String doInBackground(URL... arg0) {
		
		try {
		    URL url = arg0[0];

		    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
		    String str;
		    StringBuffer srb = new StringBuffer();
		    while ((str = in.readLine()) != null) {
		       srb.append(str);
		    }
		    in.close();
		    
		    JSONObject jObj = new JSONObject(srb.toString());
		    
		    if(!jObj.getBoolean("iE")) return jObj.getString("sID");
		    else return null;
		    
		} catch (Exception e) {e.printStackTrace();}
		
		return null;
	}
	
	@Override
	protected void onPostExecute(String result) {
        TodoActivity.instance.setSessionID(result);
    }

}
